package com.ffzx.parentweb.controller;


import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ffzx.commerce.framework.constant.RedisPrefix;
import com.ffzx.commerce.framework.controller.BaseController;
import com.ffzx.commerce.framework.dto.ResultDto;
import com.ffzx.commerce.framework.system.entity.SysUser;
import com.ffzx.commerce.framework.utils.ConfigUtil;
import com.ffzx.commerce.framework.utils.RedisUtil;
import com.ffzx.commerce.framework.utils.RedisWebUtils;
import com.ffzx.commerce.framework.utils.WebUtils;
import com.ffzx.commerce.framework.vo.SysMenuVo;
import com.ffzx.parentweb.utils.PermisstionUtil;

/**
 * 主布局controller
 * 
 * @author zhu.gj
 * @date 2016年3月1日 下午3:23:19
 * @version 1.0
 */
@Controller
public class IndexController extends BaseController {

	// 记录日志
	private final static Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	@Autowired
	private PermisstionUtil permisstionUtil;
	
	@Resource
	private RedisUtil redisUtil;

	@RequestMapping(value = "/index")
	public String index() {
		return "index";
	}

	@RequestMapping("/logout")
	public String logout(HttpServletRequest request) throws Exception {
		WebUtils.clearSession(request);
		// 被拦截器拦截处理
		return "redirect:" + ConfigUtil.getInstance().getCasServerUrl() + "/logout?service="
				+ ConfigUtil.getInstance().getCasServiceUrl();
	}
	
	/**
	 * 根据用户 获取已分配的菜单
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/queryMenuByUserId", method = RequestMethod.POST)
	@ResponseBody
	public List<SysMenuVo> getMenuListByUserId(HttpServletRequest request, HttpServletResponse response) {
		logger.debug("IndexController-getMenuListByUserId=》菜单权限-已分配的菜单");
		try {
			// 获得当前用户信息
			SysUser currentUser = RedisWebUtils.getLoginUser();
			
			//从缓存中获取菜单
			List<SysMenuVo> resultList = (List<SysMenuVo>)redisUtil.get(RedisPrefix.SYSTEM_LOGIN + RedisPrefix.USER_MENU + currentUser.getId());
			if(null != resultList){
				return resultList;
			}
			
			//使用dubbo从pms权限系统中获取用户菜单
			ResultDto resultDto = permisstionUtil.getMenuPermissionList(currentUser.getId());
			if(resultDto.getCode().equals(ResultDto.OK_CODE)) {
				return (List<SysMenuVo>)resultDto.getData();
			}
		} catch (Exception e) {
			logger.error("IndexController-getMenuListByUserId-Exception=》菜单权限-已分配的菜单-Exception", e);
		}
		return null;
	}
	
	/**
	 * 没有权限跳转页面
	 */
	@RequestMapping(value = "/noRight", method = RequestMethod.GET)
	public String noRight(HttpServletRequest request, ModelMap map) {
		String permissions = request.getParameter("permissions");
		if(null != permissions){
			map.put("permissionStr", permissions);
		}
		
		return "error/403";
	}
	
	/**
	 * 404页面
	 * @return
	 */
	@RequestMapping("*")
	public String noHandlerFound404() {
		return "error/noHandlerFound404";
	}
	@RequestMapping("*/**")
	public String noHandlerFound404more() {
		return "error/noHandlerFound404";
	}


}